package gui;

import services.ServicesUsuarios;
import com.framework.gui.GenericPanel;
import com.framework.gui.GuiUtilities;
import com.framework.gui.PresentationException;
import com.framework.services.BusinessTierException;

import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

/**
 * 
 *  
 * @author gbarce
 *
 */


public class LoginPanel extends GenericPanel {

	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField userTextField = null;
	private JPasswordField clavePasswordField = null;
	private JButton loginButton = null;
	
	private boolean loginOk;
	
	public boolean isLoginOk() {
		return loginOk;
	}

	/**
	 * This method initializes 
	 * 
	 */
	public LoginPanel() {
		super();
		initialize();
		loginOk = false;
		userTextField.requestFocus();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        jLabel1 = new JLabel();
        jLabel1.setBounds(new Rectangle(23, 50, 58, 16));
        jLabel1.setText("Clave");
        jLabel = new JLabel();
        jLabel.setBounds(new Rectangle(24, 23, 57, 16));
        jLabel.setText("Usuario");
        this.setLayout(null);
        this.setSize(new Dimension(231, 141));
        this.add(jLabel, null);
        this.add(jLabel1, null);
        this.add(getUserTextField(), null);
        this.add(getClavePasswordField(), null);
        this.add(getLoginButton(), null);
			
	}

	/**
	 * This method initializes userTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUserTextField() {
		if (userTextField == null) {
			userTextField = new JTextField();
			userTextField.setBounds(new Rectangle(88, 22, 80, 20));
		}
		return userTextField;
	}

	/**
	 * This method initializes clavePasswordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getClavePasswordField() {
		if (clavePasswordField == null) {
			clavePasswordField = new JPasswordField();
			clavePasswordField.setBounds(new Rectangle(90, 49, 79, 20));
			clavePasswordField.setEchoChar('*');
		}
		return clavePasswordField;
	}

	/**
	 * This method initializes loginButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getLoginButton() {
		if (loginButton == null) {
			loginButton = new JButton();
			loginButton.setBounds(new Rectangle(121, 81, 83, 19));
			loginButton.setText("Aceptar");
			loginButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try {
						loginUsuario();
					}
					catch(PresentationException pex) {
						GuiUtilities.showError(getParent(), pex.getMessage());
					}
				}
			});
		}
		return loginButton;
	}
	
	/**
	 * 
	 * @throws PresentationException
	 */
	private void loginUsuario() throws PresentationException {
		try {
			ServicesUsuarios services = new ServicesUsuarios();
			String user  = userTextField.getText();
			String clave = new String(clavePasswordField.getPassword());
			
			services.validarLogin(user, clave);
						
			loginOk = true;
			
			GuiUtilities.showInformation(getParent(), "Bienvenido al sistema");
			
			closeContainer();
			
	
		}
		catch(BusinessTierException btex) {
			throw new PresentationException(btex.getMessage());
		}
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
